package com.hgm.travelapp.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.hgm.travelapp.R
import com.hgm.travelapp.fragment.DeleteDialog

class RecyclerViewAdapter2(private val manager: FragmentManager, var type: Int) :
    RecyclerView.Adapter<RecyclerViewAdapter2.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return if (type == 0) {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.item_ticket, parent, false)
            MyViewHolder(view)
        } else {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.item_order, parent, false)
            MyViewHolder(view)
        }
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        when (type) {
            0 -> {
                holder.t_head.setImageResource(R.drawable.head_ticket)
                holder.t_title.text = "冷泉滩旅游度假区"
                holder.t_price.text = "￥60起"
                holder.t_delete.text = "删除"
            }
            1 -> {
                holder.o_head.setImageResource(R.drawable.head_ticket)
                holder.o_title.text = "清远星空船"
                holder.o_price.text = "￥2999"
                holder.o_time.text = "订购时间：2022年8月2日"
            }
        }


        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(position)
        }

        //holder.t_delete.setOnClickListener {
        //    //弹出dialog
        //    val dialog = DeleteDialog()
        //    dialog.show(manager,"deleteDialog")
        //    if (dialog.isCancelable){
        //        //list.remove(position)
        //        //notifyItemRemoved(position);
        //        //notifyDataSetChanged();
        //    }
        //}
    }

    override fun getItemCount(): Int {
        return 20
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        //TicketFragment页面
        var t_head = view.findViewById<ImageView>(R.id.head)
        var t_title = view.findViewById<TextView>(R.id.title)
        var t_price = view.findViewById<TextView>(R.id.price)
        var t_delete = view.findViewById<TextView>(R.id.delete)

        //OrderFragment页面
        var o_head = view.findViewById<ImageView>(R.id.head)
        var o_title = view.findViewById<TextView>(R.id.title)
        var o_price = view.findViewById<TextView>(R.id.price)
        var o_time = view.findViewById<TextView>(R.id.time)
    }

    var mOnItemClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        mOnItemClickListener = onItemClickListener
    }

    interface OnItemClickListener {
        fun onItemClick(pos: Int) {}
    }
}